/**
 * Created by duwei on 2018/11/17.
 */
var RulerWindow = Window.extend({
    _ccsFileName : res.RulerWindow_json,
    /**
     * 标题颜色
     * @type {cc.Color}
     * @const
     */
    TitleColor: cc.color('#d7842a'),
    /**
     * 详情颜色
     * @type {cc.Color}
     * @const
     */
    DetailColor: cc.color('#638f9f'),
    /**
     * title字体大小
     * @type {Number}
     * @const
     */
    TitleSize: 26,
    /**
     * detail字体大小
     * @type {Number}
     * @const
     */
    DetailSize: 22,
    /**
     * title行间距
     * @type {Number}
     * @const
     */
    TitleSpace: 20,
    /**
     * detail行间距
     * @type {Number}
     * @const
     */
    DetailSpace: 10,
    /**
     * tab数组
     * @private
     */
    _tabList: null,
    /**
     * 初始化界面
     * @private
     */
    initWindow: function () {
        var self = this;
        self._listBtnScroll = self._nodeList["_listBtnScroll"];
        self._listBtnNode = self._nodeList["_listBtnNode"];
        self._touchNode = self._nodeList["_touchNode"];
        self._touchNode.setVisible(false);
        self.btn_Hight = self._touchNode.getContentSize().height;
        self.offset_H = 5;
        self.inifJosn();
    },
    /**
     * 解析数据
     */
    inifJosn: function () {
        var self = this;
        self.mapIdMap = hall.wanfaBtn;
        self.nameMap = {};
        _.forIn(self.mapIdMap, function (value, key) {
            self.nameMap[app.map_name[key]] = value;
        });
        self.rulerLength = [];
        for (var i = 0; i < RulesData.length; i++) {
            var name = RulesData[i].Name;
            if(self.nameMap[name]){
                self.rulerLength.push({data:RulesData[i],index:i});
            }
        }
        self.count = window.inBanShu ? 2 : self.rulerLength.length;
        self.initBtn();
        self._changeData(0);
    },
    /**
     * 初始化按钮
     */
    initBtn: function () {
        var self = this;
        //实际scroll高度
        var btnScrollH = (self.count) * (self.btn_Hight + self.offset_H);
        self.rulerBtns = [];
        var interval = (self._touchNode.getContentSize().height + 5);
        for (var i = 0; i < self.count; i++) {
            (function (i) {
                var button = self._listBtnNode["touch" + i];
                if (!button) {
                    button = duplicateNode(self._touchNode);
                    button.setName("touch" + i);
                    button.setPositionY(self._touchNode.getPositionY() - interval * i);
                    var wanfaName = self.rulerLength[i].data.Name;
                    var btnDara = self.nameMap[wanfaName];
                    button.getChildByName('unselect').getChildByName('word').setTexture(btnDara.btn_normal);
                    button.getChildByName('select').getChildByName('word').setTexture(btnDara.btn_foucs);
                    button.setVisible(true);
                    var data = {};
                    data.mapName = self.rulerLength[i].data.Name;
                    button.setUserData(data);
                    self._listBtnNode.addChild(button);
                    self._listBtnNode["touch" + i] = button;
                }

                TouchUtil.setOnclickListener(button, function () {
                    self._changeData(i);
                }, {"swallowTouches": false});
            })(i);
        }
        var width = self._listBtnScroll.getContentSize().width;
        var height = btnScrollH > self._listBtnScroll.getContentSize().height ? btnScrollH : self._listBtnScroll.getContentSize().height;
        self._listBtnScroll.setInnerContainerSize(cc.size(width, height));
        self._listBtnNode.setPositionY(height);
    },
    /**
     * 改变data
     * @param id
     * @private
     */
    _changeData: function (id) {
        var self = this;
        var name = self.rulerLength[id].data.Name;
        var idx = self.rulerLength[id].index;
        var data = RulesData[idx];
        if (!data) {
            return;
        }
        var content = data.Content;
        if (!content) {
            return;
        }
        var content_scroll = this._nodeList['_dataScroll'];
        if (!content_scroll) {
            return;
        }
        content_scroll.removeAllChildren();
        var totalHeight = 0;
        for (var i = content.length - 1; i >= 0; i--) {
            var contentElement = content[i];
            var detailLabel = this._createDetailLabel(contentElement.Detail, content_scroll.getContentSize().width);
            detailLabel.x = 55;
            detailLabel.y = totalHeight;
            content_scroll.addChild(detailLabel);
            totalHeight += detailLabel.getContentSize().height;
            var titleLabel = this._createTitleLabel(contentElement.Title, content_scroll.getContentSize().width);
            titleLabel.x = 0;
            titleLabel.y = totalHeight;
            content_scroll.addChild(titleLabel);
            totalHeight += titleLabel.getContentSize().height;
        }
        content_scroll.setInnerContainerSize(cc.size(content_scroll.getContentSize().width, totalHeight));

        for (var i = 0; i < self.count; i++) {
            (function (i) {
                var button = self._listBtnNode["touch" + i];
                var data = button.getUserData();
                button.getChildByName('unselect').setVisible(data.mapName !== name);
                button.getChildByName('select').setVisible(data.mapName === name);
            })(i);
        }
        setTimeout(function () {
            content_scroll.jumpToTop()
        }, 10);
    },
    /**
     * 创建标题
     * @param {String} str
     * @param {Number} contentWidth
     * @return {cc.LabelTTF}
     * @private
     */
    _createTitleLabel: function (str, contentWidth) {
        var titleLabel = new cc.LabelTTF();
        titleLabel.setFontName(res.Default_Font);
        titleLabel.setFontSize(this.TitleSize);
        titleLabel.boundingWidth = contentWidth;
        titleLabel.setColor(this.TitleColor);
        titleLabel.setString(str);
        titleLabel.setAnchorPoint(display.LEFT_BOTTOM);
        return titleLabel;
    },
    /**
     * 创建标题
     * @param {String} str
     * @param {Number} contentWidth
     * @return {cc.LabelTTF}
     * @private
     */
    _createDetailLabel: function (str, contentWidth) {
        var titleLabel = new cc.LabelTTF();
        titleLabel.setFontName(res.Default_Font);
        titleLabel.setFontSize(this.DetailSize);
        titleLabel.boundingWidth = contentWidth - 60;
        titleLabel.setColor(this.DetailColor);
        titleLabel.setString(str);
        titleLabel.setAnchorPoint(display.LEFT_BOTTOM);
        return titleLabel;
    }
});